package com.stnet.bases;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.modules.commons.Pager;
import com.modules.util.DateHelper;
import com.stnet.entity.EnterpriseNews;
/**
 * @author Messcat
 * @version 1.1
 */
public class BaseDaoImpl extends HibernateDaoSupport {

	public BaseDaoImpl() {
	}
	
	public Pager getObjectListByClass(int pageSize, int pageNo, Class classObject, String statu) {
		Session session =null;
		Pager pager = null;
		try
		{
			session = getHibernateTemplate().getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(classObject);
			if (!"-1".equals(statu))
				criteria.add(Restrictions.eq("state", statu));
			criteria.addOrder(Order.desc("id"));
			int rowCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
			criteria.setProjection(null);
			int startIndex = pageSize * (pageNo - 1);
			criteria.setFirstResult(startIndex);
			criteria.setMaxResults(pageSize);
			List result = criteria.list();
			pager = new Pager(pageSize, pageNo, rowCount, result);
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}finally
		{
			session.close();
		}
		
		return pager;
	}
	

	public List getInfoByClassAndSize(String classObject, Long size, Long clickTimes, String isprimPhoto,
			String isCommend, String b_or_s, Long columnId, String isIndexPhoto) {
		StringBuffer SQL = new StringBuffer();
		SQL.append("from ").append(classObject).append(" as temp where 1 = 1 ").toString();
		if (!"-1".equals(isprimPhoto))
			SQL.append(" and temp.isPrimPhoto = 1 ");
		if (!"-1".equals(isIndexPhoto))
			SQL.append(" and temp.isIndexPhoto = 1 ");
		if (!"-1".equals(isCommend))
			SQL.append(" and temp.isCommend = 1 ");
		SQL.append(" and temp.state = 1 ");
		if (!"-1".equals(columnId.toString().trim()))
			SQL.append(" and temp.enterpriseColumn.id = ").append(columnId).append(" ").toString();
		String date = (new DateHelper()).getCurrentDate().toString();
		if (!"EnterpriseInfo".equals(classObject)) {
			SQL.append(" and temp.initTime <= '").append(date.trim()).append("' ");
			SQL.append(" and temp.endTime >= '").append(date.trim()).append("' ");
		}
		if (!"-1".equals(clickTimes.toString()))
			SQL.append(" order by temp.clickTimes desc");
		if (!"-1".equals(clickTimes.toString()))
			SQL.append(" ,temp.isTop desc");
		else
			SQL.append(" order by temp.isTop desc");

		if ("EnterpriseBulletin".equals(classObject))
			SQL.append(" ,temp.editeTime desc");

		SQL.append(" ,temp.id desc");

		List result = getHibernateTemplate().find(SQL.toString());
		result = result.subList(0, (int) (size <= result.size() ? size : result.size()));
		return result;
	}

	public List getLinksAndAdByClassAndSize(Class classObject, Long size) {
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(classObject);
		criteria.add(Restrictions.eq("state", "1"));
//		criteria.addOrder(Order.desc("id"));
		criteria.setProjection(null);
		criteria.setFirstResult(0);
		criteria.setMaxResults(Integer.valueOf(size.toString()).intValue());
		List result = criteria.list();
		session.close();
		return result;
	}

	public EnterpriseNews getNews(Long id) {
		List find = getHibernateTemplate().find(
				"from EnterpriseNews as e where e.state = 1 and e.isPrimPhoto = 1 and e.enterpriseColumn.id = ?", id);
		if (find.size() > 0)
			return (EnterpriseNews) find.get(0);
		else
			return null;
	}

	public List findNews(Long id) {
		List find = getHibernateTemplate().find(
				"from EnterpriseNews as e where e.state = 1 and  e.isPrimPhoto = 1 and e.enterpriseColumn.id = ?", id);
		return find;
	}
}